#include "lyrics.h"
#include "string.h"
lyrics* lyrics_list = NULL; // 全局变量，用于存储歌词列表
static unsigned long endTime = 0;
static unsigned long startTime = 60*1000*1000;
void lyrics_list_free()
{
    if (lyrics_list == NULL)
    {
        return;
    }
    lyrics* p = lyrics_list;

    while (p != NULL)
    {
        lyrics* q = p;
        p = p->next;
        free(q->text);
        free(q);
    }

    lyrics_list = NULL;
}

int create_lyrics(const char* text)
{
    lyrics_list_free();
    char* te = (char*)text;
    char* teHead = (char*)text;
    int len = 0;
    while (1)
    {
        if (*te == 0)
        {
            endTime = 0;
            return 0;
        }
        else if (*te != '\n')
        {
            len++;
        }
        else
        {
            
            // 读取到一行
            char* start = strchr(teHead, '[');
            char* end = strchr(teHead, ']');
            char* colon = strchr(teHead, ':');
            char* point = strchr(teHead, '.');
            int timeLen = end - start + 1;
            unsigned long time = 0;
            unsigned long sec = 0;
            unsigned long ms = 0;
            int per = 100;
            int i = 0;
            for ( i = 0; i < colon - start-1; i++)
            {
                time *= 10;
                time += (start[i+1] - '0') * 60 * 1000;
            }
            for (i = 0; i < point - colon-1; i++)
            {
                sec *= 10;
                sec += (colon[i+1] - '0')  * 1000;
            }
            for (i = 0; i < end - point-1; i++)
            {
                ms += (point[i + 1] - '0') * per;
                per /= 10;
            }
            
            time += sec + ms;


            lyrics* new_lyrics = (lyrics*)malloc(sizeof(lyrics));
            if(new_lyrics == NULL)
            {
                return 1;
            }
            new_lyrics->text = (char*)malloc(len- timeLen + 1);
            memcpy(new_lyrics->text,teHead+ timeLen,len- timeLen);
            if(new_lyrics->text == NULL)
            {
                return 2;
            }
            new_lyrics->text[len- timeLen] = 0;
            new_lyrics->startTime = time;
            new_lyrics->next = lyrics_list;
            if (lyrics_list != NULL)
            {
                lyrics_list->prev = new_lyrics;
            }
            
            new_lyrics->prev = NULL;

            lyrics_list = new_lyrics;

            teHead += len+1;
            len = 0;
            
        }
        te++;
    }

    return 0;
}

/**
 * @brief Get the lyric text object
 * 
 * @param time 
 * @param now 
 * @param next 
 * @param next2 
 * @return int 0 匹配成功需要更新，1:匹配失败,2:不需要更新
 */
int get_lyric_text(int time, char** now, char** next, char** prev)
{
    lyrics* p = lyrics_list;
    if(endTime > time && startTime < time)
    {
        return 2;
    }
    *now = 0;
    *next = 0;
    *prev = 0;
    while (p != NULL)
    {
        if (p->startTime <= time)
        {
            *now = p->text;
            startTime = p->startTime;
            if (p->prev != NULL)
            {
                
                endTime = p->prev->startTime;
                *next = p->prev->text;
            }
            else
            {
                endTime = 60*60*1000;
            }
            if(p->next != NULL)
            {
                *prev = p->next->text;
            }
            return 0;
        }
        p = p->next;
    }
    return 1;
}